<template>
  <div class="full-box shadow-box padding-box">
    <OperateCommon :showSearch="true" @search="initData">
      <template #condition>
        <div class="condition-item">
          <el-date-picker
            style="width: 130px"
            v-model="queryParams.plan_year"
            type="year"
            value-format="yyyy"
            placeholder="申报年份"
            clearable
          >
          </el-date-picker>
        </div>
        <div class="condition-item">
          <el-select
            style="width: 150px"
            v-model="queryParams.industry_type"
            value-key=""
          >
            <el-option
              v-for="item in industryOptions"
              :key="item.id"
              :label="item.dict_value"
              :value="item.id"
            ></el-option>
          </el-select>
        </div>
        <div class="condition-item">
          <el-input
            v-model="queryParams.project_name"
            placeholder="按项目名称查询"
            clearable
          ></el-input>
        </div>
        <div class="condition-item">
          <el-input
            v-model="queryParams.create_user_name"
            placeholder="上报人"
            clearable
          ></el-input>
        </div>
      </template>
      <template #operate> </template>
    </OperateCommon>
    <div class="table-wrapper">
      <BaseTable
        :columns="columns"
        :showSort="true"
        :tableData="tableData"
        :pages="{
          show: true,
          count: count,
        }"
        :operate="{
          show: true,
          width: 450,
        }"
        @pagination="pageChange"
      >
        <template #operate="{ current }">
          <el-button
            style="width: 100px"
            :type="current.row.label_important == 0 ? '' : 'danger'"
            size="mini"
            @click="setType(current.row)"
            >{{
              current.row.label_important == 0 ? "转重大项目" : "转非重大项目"
            }}</el-button
          >
          <el-button type="info" size="mini" @click="openDialog(1, current.row)"
            >领导联系</el-button
          >
          <el-button type="info" size="mini" @click="openDialog(2, current.row)"
            >项目标签</el-button
          >
          <el-button type="primary" size="mini" @click="linkWhere(current.row)"
            >详情</el-button
          >
        </template>
      </BaseTable>
    </div>
    <el-dialog
      v-if="current"
      title="领导联系"
      width="500px"
      :visible.sync="dialog.pLeader"
      :close-on-click-modal="false"
      @close="dialog.pLeader = false"
    >
      <div v-loading="loading">
        <div class="cur_pro">
          <label>当前项目：</label>
          {{ current.project_name }}
        </div>
        <el-select
          v-model="current.leader_id"
          value-key=""
          placeholder=""
          clearable
          multiple
          filterable
        >
          <el-option
            v-for="item in user"
            :key="item.id"
            :label="item.user_name"
            :value="item.id"
          >
          </el-option>
        </el-select>

        <el-button style="margin-left: 10px" type="primary" @click="saveUser"
          >保存</el-button
        >
        <el-button type="info" @click="dialog.pLeader = false">关闭</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-if="current"
      title="项目标签"
      width="500px"
      :visible.sync="dialog.pTag"
      :close-on-click-modal="false"
      @close="dialog.pTag = false"
    >
      <div v-loading="loading">
        <div class="cur_pro">
          <label>当前项目：</label>
          {{ current.project_name }}
        </div>
        <el-select
          v-model="currentTags"
          value-key=""
          placeholder=""
          clearable
          filterable
          multiple
        >
          <el-option
            v-for="(item, index) in tags"
            :key="index"
            :label="item.dict_value"
            :value="item.dict_value"
          >
          </el-option>
        </el-select>

        <el-button style="margin-left: 10px" type="primary" @click="saveTags"
          >保存</el-button
        >
        <el-button type="info" @click="dialog.pTag = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import BaseTable from "@/components/Common/BaseTable.vue";
import OperateCommon from "@/components/Common/OperateCommon.vue";
import LinkTo from "@/mixins/LinkTo";
import commonList from "@/mixins/commonList";
import { getYearPlanReleaseList } from "@/api/YearPlan/Index";
import {
  getTagList,
  postSetYearPlanLabel,
  setProjectLeader,
  setProjectTag,
} from "@/api/BackendManage/Index";
import { getUserList } from "@/api/System/User";
import m_industry from "@/mixins/industry";
export default {
  mixins: [LinkTo, commonList, m_industry],
  components: { BaseTable, OperateCommon },
  data() {
    return {
      columns: columns,
      tableData: [],
      count: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        project_name: "",
        status: "",
        plan_year: "",
        industry_type: "",
        create_user_name: "",
      },
      dialog: {
        pTag: false,
        pLeader: false,
      },
      loading: false,
      tags: [],
      current: null,
      currentTags: [],

      user: [],
    };
  },
  methods: {
    initData() {
      getYearPlanReleaseList(this.queryParams).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data;
          this.count = res.num;
        }
      });
    },
    pageChange(val) {
      this.queryParams.pageNum = val.pageNum;
      this.queryParams.pageSize = val.pageSize;
      this.initData();
    },
    linkWhere(item) {
      this.linkTo(`/yearplan/declare/${item.id}/999`);
    },
    setType(item) {
      let label = 0;

      if (item.label_important == 0) {
        label = 1;
      }

      postSetYearPlanLabel({
        id: item.id,
        label_important: label,
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("设置成功");
          this.initData();
        }
      });
    },
    initTags() {
      this.loading = true;
      getTagList().then((res) => {
        if (res.code == 0) {
          this.tags = res.data;
        }
        this.loading = false;
      });
    },
    initUser() {
      this.loading = true;
      getUserList({ pageNum: 1, pageSize: 9999 }).then((res) => {
        if (res.code == 0) {
          this.user = res.data;
        }
        this.loading = false;
      });
    },
    openDialog(type, row) {
      this.currentTags = null;
      this.current = row;

      if (type == 1) {
        this.dialog.pLeader = true;
        this.initUser();
        this.current.leader_id = row.leader_id.split(",");
      }

      if (type == 2) {
        this.dialog.pTag = true;
        this.initTags();

        if (this.current.label != "") {
          this.currentTags = this.current.label.split(",");
        }
      }
    },
    saveTags() {
      setProjectTag({
        id: this.current.id,
        label: this.currentTags.join(","),
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("设置成功");
          this.dialog.pTag = false;
          this.initData();
        }
      });
    },
    saveUser() {
      setProjectLeader({
        id: this.current.id,
        leader_id: this.current.leader_id.join(","),
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("设置成功");
          this.dialog.pLeader = false;
          this.initData();
        }
      });
    },
  },
  created() {
    this.initData();
  },
};

const columns = [
  {
    prop: "plan_year",
    label: "申报年份",
    width: "100",
    minWidth: "",
    align: "",
  },
  {
    prop: "project_name",
    label: "项目名称",
    width: "",
    minWidth: "300",
    align: "",
  },
  {
    prop: "invest_type",
    label: "投资类型",
    width: "",
    minWidth: "100",
    align: "",
  },
  {
    prop: "industry_value",
    label: "行业分类",
    width: "",
    minWidth: "100",
    align: "",
  },
  {
    prop: "investment",
    label: "总投资",
    width: "",
    minWidth: "100",
    align: "",
  },
  {
    prop: "status_text",
    label: "审核状态",
    width: "",
    minWidth: "80",
    align: "",
  },
];
</script>

<style lang="scss" scoped>
.cur_pro {
  height: 50px;
  line-height: 50px;
  label {
    font-weight: 600;
  }
}
</style>
